package com.lyj.dao.impl;

import java.sql.ResultSet;

import com.lyj.dao.UserDao;
import com.lyj.pojo.User;
import com.lyj.pojo.UserQueryModel;
import com.lyj.util.base.BaseDaoImpl;
import com.lyj.util.base.BaseQueryModel;

/**
* 
* @author 李艺江
* @version 创建时间：2018年11月27日 下午5:27:39
*/

public class UserDaoImpl extends BaseDaoImpl implements UserDao{

	@Override
	public String getInsertSql(Object data) {
		//向下转型
		User user = (User)data;
		return "insert into t_user(name,pwd,online) " +
	    " values('" +user.getName()+ "','" +user.getPwd()+ "'," +user.getOnline() + ")";
	}

	@Override
	public String getFindAllSql() {		
		return "select * from t_user";
	}

	//构建一个带条件的sql语句
	//BaseQueryModel queryModel记录了条件的值
	@Override
	public String getFindConditionSql(BaseQueryModel queryModel) {
		//向下转型
		UserQueryModel qm = (UserQueryModel)queryModel;
		StringBuilder sb=new StringBuilder();
		sb.append("select * from t_user");
		sb.append(" where 1=1 ");
		if(qm.getName()!=null && qm.getName().trim().length()>0){
			sb.append(" and name='"+qm.getName()+"'");
		}
		if(qm.getPwd()!=null && qm.getPwd().trim().length()>0){
			sb.append(" and pwd='"+qm.getPwd()+"'");
		}
		if(qm.getOnline()>0){
			sb.append("and online="+qm.getOnline());
		}
		return sb.toString();
	}

	@Override
	public Object rsToObject(ResultSet rs) throws Exception {
		//1 创建User实体类对象
		User user = new User();
		//2 从ResultSet结果集中读取一条记录，将每个字段的值
		//复制给User对象的对应属性
		user.setId(rs.getInt("id"));
		user.setName(rs.getString("name"));
		user.setPwd(rs.getString("pwd"));
		user.setOnline(rs.getInt("online"));
		
		return user;
	}

	@Override
	public String getUpdateSql(Object data) {
		User user = (User)data;
		return "update t_user set name='"+user.getName()+"',pwd='"+user.getPwd()+"',"+
	    "online="+user.getOnline()+" where id=" + user.getId();
	}

	@Override
	public String getDeleteSql(int id) {
		return "delete from t_user where id="+id;
	}

}
